package com.youyunsoft.commons;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.youyunsoft.commons.myview.MyViewActivity;
import com.youyunsoft.commons.nested_list.NestedListActivity;
import com.youyunsoft.commons.recyclerview_expandlistview.RecyclerExpandActivity;
import com.youyunsoft.commons.utils.Util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import exercise.database.DataBaseActivity;

public class MainActivity extends BaseActivity {

    RecyclerView mRecyclerParent;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Util.INSTANCE.init(this.getApplicationContext());


        initView();
    }




    private void initView() {

        final String[] titles
                = {"Recyclerview模拟Expandablelistview",
                "ListView嵌套Listview",
                "自定义view",
                "数据库"};


        mRecyclerParent = bindView(R.id.recycler_parent);
        mRecyclerParent.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerParent.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

        ListAdapter adapter = new ListAdapter(this, Arrays.asList(titles));
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

                ToastMsg(titles[position]);

                Intent intent = null;
                switch (position) {

                    case 0://Recyclerview模拟Expandablelistview
                        startActivity(RecyclerExpandActivity.class);
                        break;

                    case 2://ListView 嵌套Listview
                        if (true) {
                            Util.INSTANCE.toast("施工中");
                            return;
                        } else {
                            startActivity(NestedListActivity.class);
                        }
                        break;

                    case 5:
                        startActivity(MyViewActivity.class);
                        break;

                    case 6:
                        startActivity(DataBaseActivity.class);
                        break;


                }
//                if (intent != null) {
//                    startActivity(intent);
//                }
            }
        });

        mRecyclerParent.setAdapter(adapter);

    }

    private void startActivity(Class clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }


    class ListAdapter extends RecyclerView.Adapter {

        class ViewHolder extends RecyclerView.ViewHolder {

            View mItemView;

            TextView tv_title;

            public ViewHolder(View itemView) {
                super(itemView);
                mItemView = itemView;
                tv_title = bindView(mItemView, R.id.tv_title);
            }

            public View getItemView() {
                return mItemView;
            }

        }


        List<String> mData = new ArrayList<>();
        private Context mContext;
        OnItemClickListener mOnItemClickListener;

        public ListAdapter(Context context, List<String> data) {

            mContext = context;
            mData.clear();
            mData.addAll(data);

        }

        public void setOnItemClickListener(OnItemClickListener listener) {
            mOnItemClickListener = listener;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            LayoutInflater layoutInflater = LayoutInflater.from(mContext);
            View parentLayout = layoutInflater.inflate(R.layout.activity_main_list_item, parent, false);

            return new ViewHolder(parentLayout);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            ViewHolder vh = (ViewHolder) holder;

            ViewGroup.LayoutParams layoutParams = vh.itemView.getLayoutParams();
            layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;


            vh.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickListener.onItemClick(v, position);
                }
            });

            String title = mData.get(position);

            vh.tv_title.setText(title);

        }

        @Override
        public int getItemCount() {
            return mData.size();
        }
    }


}
